<?php
namespace app\admin\controller;

use app\common\model\Vercode as VercodeModel;
use app\common\model\Topic as TopicModel;
use app\common\model\Ping as PingModel;
use app\common\model\Member as MemberModel;
use app\common\model\Message as MessageModel;
/**
 * 信息控制器
 * Class Message
 * @package app\admin\controller
 */
class Message extends Base
{
    /**
     * [getIndex  消息列表]
     * @param $data ['type'] 1短信消息记录 2论坛消息记录 3私信消息记录4屏蔽词设置
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getIndex()
    {
        $data = input('param.');
        $this->getPageAndSize($data);
        $map = [];
        $user = $this->user;
        if(!empty($data['type'])) {
            if ($data['type'] == 1) {
                $model = new VercodeModel();
                $mmodel = new MemberModel;
                if (!empty($data['key'])) {
                    $map['am.tel|am.username'] = ['like', "%" . $data['key'] . "%"];
                }
                if(!empty($data['status'])){
                    $map['status'] = $data['status'];
                }
                if(!empty($data['start']) && !empty($data['end'])){
                    $map['addtime'] = ['between',[strtotime($data['start']),strtotime($data['end'])]];
                }
                try{
                    if(!empty($data['key'])){
                        $count = $model->getCount($map);//满足条件的总数
                        $allpage = intval(ceil($count / $this->size));//总页数
                        $lists = $model->getlist($map,$this->from, $this->size);
                    }else {
                        $count = $model->getCounts($map);//满足条件的总数
                        $allpage = intval(ceil($count / $this->size));//总页数
                        $lists = $model->getlists($map, $this->from, $this->size);
                        foreach($lists as &$v){
                            $member = $mmodel->findData($v['tel'],'tel','username');
                            if(empty($member)){
                                $v['username'] = '未知';
                            }else{
                                $v['username'] = $member['username'];
                            }
                        }
                    }
                }catch(\Exception $e){
                    return admin_json(-1, '获取失败'.$e->getMessage());
                }

                writelog('查看短信消息记录',1,$user);
                return admin_json(1,'获取成功',[
                    'count' => $count,//总记录数
                    'allpage' => $allpage,  //总页数
                    'lists' => $lists,  //列表数据
                ]);die;
            }elseif($data['type'] == 2){
                $model = new TopicModel();
                $mmodel = new MemberModel();
                if (!empty($data['con'])) {
                    $map['content'] = ['like', "%" . $data['con'] . "%"];
                }
                $map['id'] = ['not between',[1443,1448]];
                if(!empty($data['states'])){
                    $map['states'] = $data['states'];
                }
                if(!empty($data['start']) || !empty($data['end'])){
                    $map['addtime'] = ['between',[strtotime($data['start']),strtotime($data['end'])]];
                }
                try{
                    $count = $model->getAllcount($map);//满足条件的总数
                    $allpage = intval(ceil($count / $this->size));//总页数
                    $lists = $model->getAll($map,$this->from, $this->size);

                    foreach ($lists as $key => $v) {
                        $lists[$key]['ctype'] = $v['parid'] > 0 ? '回复' : '原创';
                        $lists[$key]['addtime'] = date('Y-m-d H:i:s', $lists[$key]['addtime']);

                        $lists[$key]['content'] = urldecode($v['content']);

                        $res = $model->findOne1($v['id'],'parid');

                        if(!empty($res)){
                            if($res['id']<1443) {
                                $lists[$key]['reply'] = urldecode($res['content']);
                            }
                        }

                        if($v['parid'] == 0){
                            $user1 = $mmodel->findData($v['memid'],'id','id,tel,username');
                        }else{
                            $user1 = $mmodel->findData($v['comment_uid'],'id','id,tel,username');
                        }

                        $lists[$key]['tel'] = $user1['tel'];
                        $lists[$key]['username'] = $user1['username'];

                        if (!empty($v['imgs'])) {
                            $arr2 = explode(',',$v['imgs']);
                            $lists[$key]['imgs'] = '';
                            foreach ($arr2 as $key2 => $subimg) {
                                if(!empty($subimg))
                                {
                                    $http = substr($subimg,0,4);
                                    if($http != 'http'){
                                        if($lists[$key]['imgs'] == ''){
                                            $lists[$key]['imgs'] .= config('domain_name').'/'.$subimg;
                                        }else{
                                            $lists[$key]['imgs'] .= ','.config('domain_name').'/'.$subimg;
                                        }
                                    }else{
                                        if($lists[$key]['imgs'] == ''){
                                            $lists[$key]['imgs'] .= $subimg;
                                        }else{
                                            $lists[$key]['imgs'] .= ','.$subimg;
                                        }
                                    }
                                }
                            }
                            $lists[$key]['imgs'] = explode(',',$lists[$key]['imgs']);
                        }
                    }
                }catch(\Exception $e){
                    return admin_json(-1, '获取失败'.$e->getMessage());
                }

                writelog('查看论坛消息记录',1,$user);
                return admin_json(1,'获取成功',[
                    'count' => $count,//总记录数
                    'allpage' => $allpage,  //总页数
                    'lists' => $lists,  //列表数据
                ]);
            }elseif($data['type'] == 3) {
                $model = new MessageModel();
                if (!empty($data['con'])) {
                    $map['am1.username|am2.username'] = ['like', "%" . $data['con'] . "%"];
                }
                if(!empty($data['start']) && !empty($data['end'])){
                    $map['r.addtime'] = ['between',[strtotime($data['start']),strtotime($data['end'])]];
                }
                $count = $model->getAcount($map);//满足条件的总数
                $allpage = intval(ceil($count / $this->size));//总页数
                $lists = $model->getAlist($map,$this->from, $this->size);
                foreach($lists as $key=>$v){
                    $arr = urldecode($v['content']);
                    $lists[$key]['content'] = $arr;
                    $lists[$key]['addtime'] = date('Y:m:d H:i:s',$v['addtime']);
                }
                writelog('查看私信消息记录',1,$user);
                return admin_json(1,'获取成功',[
                    'count' => $count,//总记录数
                    'allpage' => $allpage,  //总页数
                    'lists' => $lists,  //列表数据
                ]);
            }elseif($data['type'] == 4) {
                $model = new PingModel();
                $count = $model->getCount();//满足条件的总数
                $allpage = intval(ceil($count / $this->size));//总页数
                $lists = $model->getlist($this->from, $this->size);
                writelog('查看屏蔽词',1,$user);
                return admin_json(1,'获取成功',[
                    'count' => $count,//总记录数
                    'allpage' => $allpage,  //总页数
                    'lists' => $lists,  //列表数据
                ]);
            }
        }
    }
    /**
     * [postStatus  修改]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postStatus()
    {
        $model = new TopicModel();
        $data = input('param.');
        $user = $this->user;
        try{
            $model->editData($data);
        }catch(\Exception $e){
            return admin_json(-1, '修改状态'.$e->getMessage());
        }
        if($data['states'] == 1){
            $name = '启用';
        }elseif($data['states'] == 2){
            $name = '禁用';
        }
        writelog($name.'论坛消息'.$data['id'],1,$user);
        return admin_json(1,'修改成功');
    }
    /**
     * [postAdd  添加]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postAdd()
    {
        $model = new PingModel();
        $data = input('param.');
        $user = $this->user;
        try{
            $data['addtime'] = time();
            $id = $model->addData($data);
        }catch(\Exception $e){
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        writelog('添加屏蔽词'.$id,1,$user);
        return admin_json(1,'添加成功');
    }
    /**
     * [postAdd  修改]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postEdit()
    {
        $model = new PingModel();
        $data = input('param.');
        $user = $this->user;
        try{
            $model->editData($data);
        }catch(\Exception $e){
            return admin_json(-1, '修改失败'.$e->getMessage());
        }
        writelog('修改屏蔽词'.$data['id'],1,$user);
        return admin_json(1,'修改成功');
    }
    /**
     * [postAdd 删除]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getDel()
    {
        $model = new PingModel();
        $data = input('param.');
        $user = $this->user;
        try{
            $model->deleteTrueData($data['id']);
        }catch(\Exception $e){
            return admin_json(-1, '删除失败'.$e->getMessage());
        }
        writelog('删除屏蔽词'.$data['id'],1,$user);
        return admin_json(1,'删除成功');
    }
}